﻿using JetBrains.Annotations;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Volo.Abp.Data;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.ObjectExtending;

namespace TianShu.TenantManagement.Dal.Abstracts.Entities
{
    [SugarTable("tianshu_tenant")]
    public class TenantEntity : FullAuditedAggregateRoot<Guid>
    {

        static TenantEntity()
        {
            ObjectExtensionManager.Instance.AddOrUpdate<TenantEntity>(opt =>
            {
                opt.AddOrUpdateProperty<string>("ContactUserName", info => info.CheckPairDefinitionOnMapping = false);
                opt.AddOrUpdateProperty<string>("ContactUserPhone", info => info.CheckPairDefinitionOnMapping = false);
                opt.AddOrUpdateProperty<string>("ContactAddress", info => info.CheckPairDefinitionOnMapping = false);
            });
        }
        [SugarColumn(IsNullable = false, Length = 64)]
        public virtual string Code { get; protected set; }
        [SugarColumn(IsNullable = false, Length = 64)]
        public virtual string Name { get; protected set; }

        [SugarColumn(ColumnDataType = "longtext", IsJson = true)]
        public override ExtraPropertyDictionary ExtraProperties { get; set; }

        [SugarColumn(IsIgnore = true)]
        public virtual List<TenantConnectionStringEntity> ConnectionStrings { get; set; }

        public TenantEntity()
        {
        }

        public TenantEntity(Guid id, [NotNull] string code, [NotNull] string name)
            : base(id)
        {
            SetName(name);
            SetCode(code);

            ConnectionStrings = new List<TenantConnectionStringEntity>();
        }

        [CanBeNull]
        public virtual string FindDefaultConnectionString()
        {
            return FindConnectionString(Volo.Abp.Data.ConnectionStrings.DefaultConnectionStringName);
        }

        [CanBeNull]
        public virtual string FindConnectionString(string name)
        {
            return ConnectionStrings.FirstOrDefault(c => c.Name == name)?.Value;
        }

        public virtual void SetDefaultConnectionString(string connectionString)
        {
            SetConnectionString(Volo.Abp.Data.ConnectionStrings.DefaultConnectionStringName, connectionString);
        }

        public virtual void SetConnectionString(string name, string connectionString)
        {
            var tenantConnectionString = ConnectionStrings.FirstOrDefault(x => x.Name == name);

            if (tenantConnectionString != null)
            {
                tenantConnectionString.SetValue(connectionString);
            }
            else
            {
                ConnectionStrings.Add(new TenantConnectionStringEntity(Id, name, connectionString));
            }
        }

        public virtual void RemoveDefaultConnectionString()
        {
            RemoveConnectionString(Volo.Abp.Data.ConnectionStrings.DefaultConnectionStringName);
        }

        public virtual void RemoveConnectionString(string name)
        {
            var tenantConnectionString = ConnectionStrings.FirstOrDefault(x => x.Name == name);

            if (tenantConnectionString != null)
            {
                ConnectionStrings.Remove(tenantConnectionString);
            }
        }

        public virtual void SetName([NotNull] string name)
        {
            Name = Volo.Abp.Check.NotNullOrWhiteSpace(name, nameof(name), 64);
        }
        public virtual void SetCode([NotNull] string code)
        {
            Name = Volo.Abp.Check.NotNullOrWhiteSpace(code, nameof(code), 64);
        }
    }
}
